package com.zzptc.easyshop.dao;

import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.vo.Users;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDao implements IDAO {
    //登陆
    public boolean login(VO vo)throws SQLException{
       //获取数据库连接
        IDB db=new MysqlDB();
        Connection conn=db.getConnnection();
        //找一个工具
        String sql="select * from t_easybuy_user where eu_user_name=? and eu_password=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        //给？注入值
        Users u=(Users)vo;
        ps.setString(1,u.getEu_user_name());
        ps.setString(2,u.getEu_password());
        //要工具完成查询工作
        ResultSet rs=ps.executeQuery();
        if (rs.next()){
            return true;

        }

        //释放对象   关闭连接
        return false;
    }

//注册
    public boolean insert(VO vo) throws SQLException {
        //获取数据库连接
        IDB db=new MysqlDB();
        Connection conn=db.getConnnection();
        //用工具操作数据库
       PreparedStatement ps= conn.prepareStatement("insert into t_easybuy_user(eu_user_id,eu_user_name,eu_password)values(?,?,?)");
       //给？注入值
        Users users=(Users)vo;
        ps.setString(1,users.getEu_user_id());
        ps.setString(2,users.getEu_user_name());
        ps.setString(3,users.getEu_password());
        //操作数据库
        int i=ps.executeUpdate();
        //ps释放 关闭连接
        ps.close();
        db.closeConn();
        return i>0;
    }

    @Override
    public List getAll() throws SQLException {
        return null;
    }


    @Override
    public boolean deleteByID(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean update(VO vo) throws SQLException {
        return false;
    }

    @Override
    public VO getByID(VO vo) throws SQLException {
        return null;
    }
//测试
    public static void main(String[] args) {
        UserDao u=new UserDao();
        Users user=new Users();
        user.setEu_user_id("3");
        user.setEu_user_name("bbb");
        user.setEu_password("123");
//        try {
//           if(u.insert(user)){
//               System.out.println("succ");
//           }else{
//               System.out.println("error");
//           }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        try {
            System.out.println(u.login(user));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
